package cn.zhuama.org.controller;

import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Bookde;
import cn.zhuama.org.query.BookdeQuery;
import cn.zhuama.org.service.BookdeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bookde")
public class BookdeController {
    @Autowired
    BookdeService bookdeService;
    //分页
    @PostMapping
    public PageList<Bookde> queryPage(@RequestBody BookdeQuery query){
        return bookdeService.queryPage(query);
    }

    // 新增  修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Bookde bookde){
        try {
            if(bookde.getDid()==null){
                bookdeService.add(bookde);
            }else {
                bookdeService.update(bookde);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @DeleteMapping("/{did}")// @return   delete http://localhost:8080/department/1
    @ApiOperation(value = "删除接口")
    @Transactional
    public AjaxResult del(@PathVariable("did") Long id){
        try {
            bookdeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            bookdeService.patchDel(ids);
            return  new AjaxResult(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

//
    //查所有
    @GetMapping
    public List<Bookde> findAll(){
        return bookdeService.findAll();
    }
//
//


//




}
